﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System.Configuration;
using System.Diagnostics.Contracts;
using RaisingForce.Raise.Presentation.Localization;

namespace RaisingForce.RaiseHardwareTest.Configuration
{
    sealed internal class LocalizationLanguageElement :
        ConfigurationElement
    {
        #region Constructors

        public LocalizationLanguageElement()
        { }

        public LocalizationLanguageElement(
            string cultureName,
            string displayName = null)
        {
            this.CultureName = cultureName;
            this.DisplayName = displayName;
        }

        #endregion

        #region Configuration Properties

        private const string propCultureName = "name";

        [ConfigurationProperty(
            propCultureName,
            IsRequired = false,
            IsKey = true,
            DefaultValue = LocalizationLanguage.InvariantCultureName)]
        public string CultureName
        {
            get { return (string)this[propCultureName]; }
            set { this[propCultureName] = value; }
        }

        private const string propDisplayName = "text";

        [ConfigurationProperty(propDisplayName, IsRequired = false)]
        public string DisplayName
        {
            get { return (string)this[propDisplayName]; }
            set { this[propDisplayName] = value; }
        }

        private const string propIconPath = "icon";

        [ConfigurationProperty(
            propIconPath,
            IsRequired = false,
            DefaultValue = null)]
        public string IconPath
        {
            get { return (string)this[propIconPath]; }
            set { this[propIconPath] = value; }
        }

        #endregion

        #region Properties

        public bool IsInvariant
        {
            get
            {
                string cultureName = this.CultureName;
                return string.IsNullOrEmpty(cultureName)
                    || cultureName == LocalizationLanguage.InvariantCultureName;
            }
        }

        #endregion

        #region Public CreateLocalizationLanguage Method

        public LocalizationLanguage CreateLocalizationLanguage()
        {
            Contract.Ensures(Contract.Result<LocalizationLanguage>() != null);
            string displayNameProperty = this.DisplayName;
            string displayName = !string.IsNullOrEmpty(displayNameProperty) ?
                this.DisplayName :
                null;
            return this.IsInvariant ?
                LocalizationLanguage.CreateInvariant(displayName, this.IconPath) :
                LocalizationLanguage.Create(this.CultureName, displayName, this.IconPath);
        }

        #endregion
    }
}
